package com.guys8.cemsbackend.controller;


import com.guys8.cemsbackend.DTO.InschoolInformation;
import com.guys8.cemsbackend.DTO.Response;
import com.guys8.cemsbackend.DTO.UserInformation;
import com.guys8.cemsbackend.entity.Inschool;
import com.guys8.cemsbackend.entity.User;
import com.guys8.cemsbackend.service.impl.InschoolServiceImpl;
import com.guys8.cemsbackend.service.impl.UserServiceImpl;
import com.guys8.cemsbackend.utils.JWTutil.JWTUtil;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * (Inschool)表控制层
 *
 * @author breeze
 * @since 2022-08-25 09:50:55
 */
@RestController
@Api(tags=" Inschool-信息相关接口")
@RequestMapping("/Inschool")
public class InschoolController {
    /**
     * 服务对象
     */
    @Autowired
    private InschoolServiceImpl inschoolService;
    @Autowired
    private UserServiceImpl userService;


    @PostMapping("/addInschool/{inTime}&&{inCause}&&{from}&&{vehicle}&&{hcode}&&{riskPlace}&&{bodyTemperature}")
    @ApiOperation("上传入校申请信息(token)")
    public Response addInSchool(@RequestHeader String schoolid , @PathVariable Date inTime, @PathVariable String inCause,
                                @PathVariable String from, @PathVariable String vehicle, @PathVariable String hcode, @PathVariable String riskPlace, @PathVariable float bodyTemperature) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Inschool inschool=new Inschool(user.getUserId(),inTime,inCause,from,vehicle,hcode,riskPlace,bodyTemperature);
        if(inschoolService.save(inschool))
            return Response.restResult(1);
        else return Response.restResult(0);
    }

    @DeleteMapping("/deleteInschoolBySchoolid/")
    @ApiOperation("删除最近一次入校信息(token)")
    public Response logoutInSchool(@RequestHeader String schoolid) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user =userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Inschool inschool=inschoolService.getusernowIn(user.getUserId());
        if(inschool!=null) {
            if(inschoolService.removeById((inschoolService.getusernowIn(user.getUserId())).getInId()))
                return Response.restResult(1);
        }
        return Response.restResult(0);

    }

    @GetMapping("/getBySchoolid/")
    @ApiOperation("按Token查找最近一次入校信息(token)")
    public Response getInSchoolbyToken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getInSchool(schoolid);
    }

    @PostMapping("/getBySchoolid/find/{schoolid}")
    @ApiOperation("按schoolId查找最近一次入校信息")
    public Response getInSchool(@PathVariable String schoolid){
        User user =userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Inschool inschool=inschoolService.getusernowIn(user.getUserId());
        UserInformation userIn = new UserInformation(user);
        return Response.restResult(1,userIn,new InschoolInformation(user,inschool));
    }

    @GetMapping("/getAllBySchoolid/")
    @ApiOperation("按token查找所有入校信息(token)")
    public Response getALLInSchoolbtToken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getALLInSchool(schoolid);
    }

    @PostMapping("/getAllBySchoolid/find/{schoolid}")
    @ApiOperation("按schoolId查找所有入校信息")
    public Response getALLInSchool(@PathVariable String schoolid){
        User user =userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        List<Inschool> inschools=inschoolService.getallById(user.getUserId());
        List<InschoolInformation> inschoolInformations = new LinkedList<>();
        for(Inschool inschool:inschools){
            inschoolInformations.add(new InschoolInformation(user,inschool));
        }
        UserInformation userIn = new UserInformation(user);
        return Response.restResult(1,userIn,inschoolInformations);
    }

    @GetMapping("/getById/{InschoolId}")
    @ApiOperation("//按Id查找信息(数据库测试展示接口)")
    public InschoolInformation getoneById(@PathVariable Integer InschoolId) {
        Inschool inschool = inschoolService.getById(InschoolId);
        User user = userService.getById(inschool.getUserId());
        return new InschoolInformation(user,inschool);
    }
  
}

